const Validator = require('validator')
const isEmpty = require('./is-Empty')

module.exports = (data) => {
    console.log('start ==', data);
    let errors = {}
    data.username = !isEmpty(data.username) ? data.username : '';
    data.email = !isEmpty(data.email) ? data.email : '';
    data.password = !isEmpty(data.password) ? data.password : '';
    data.password2 = !isEmpty(data.password2) ? data.password2 : '';
    console.log('end ==', data);
    if (Validator.isEmpty(data.username)) {
        errors.username = '名字不能为空'
    }
    if (!Validator.isLength(data.username, 2, 30)) {
        errors.username = '名字最小2位 最长30位'
    }
    if (Validator.isEmpty(data.email)) {
        errors.email = 'email不能为空'
    }
    if (!Validator.isEmail(data.email)) {
        errors.email = '邮箱不合法'
    }
    if (Validator.isEmpty(data.password)) {
        errors.password = 'password不能为空'
    }
    if (!Validator.isLength(data.password, 6, 30)) {
        errors.password = '密码最小6位 最长30位'
    }
    if (Validator.isEmpty(data.password2)) {
        errors.password2 = 'password2不能为空'
    }
    if (!Validator.equals(data.password, data.password2)) {
        errors.password2 = '两次密码不一致'
    }
    return {
        errors,
        isValid: isEmpty(errors)
    }
}